﻿
namespace Apache.Cassandra.Client.Diagnostics {
    using System;
    using System.Diagnostics;

    sealed internal class CassandraCounter {
        private PerformanceCounter instance;

        internal CassandraCounter(string categoryName, string instanceName, CounterCreationData counterData) {
            PerformanceCounter instance = null;
            try {
                instance = new PerformanceCounter();
                instance.CategoryName = categoryName;
                instance.CounterName = counterData.CounterName;
                instance.InstanceName = instanceName;
                instance.InstanceLifetime = PerformanceCounterInstanceLifetime.Process;
                instance.ReadOnly = false;
                instance.RawValue = 0;
            } catch (Exception) {
                if (null != instance) {
                    instance.Dispose();
                }

                throw;
            }
            this.instance = instance;
        }

        internal void Increment() {
            PerformanceCounter instance = this.instance;
            if (null != instance) {
                instance.Increment();
            }
        }

        internal void IncrementBy(long value) {
            PerformanceCounter instance = this.instance;
            if (null != instance) {
                instance.IncrementBy(value);
            }
        }

        internal void Decrement() {
            PerformanceCounter instance = this.instance;
            if (null != instance) {
                instance.Decrement();
            }
        }
    }
}
